<?php
/**
 * Created by PhpStorm.
 * User: alex
 * Date: 2018/11/6
 * Time: 下午9:07
 */

namespace BaseBundle\Services;
use BaseBundle\Controller\BaseController;
use RedUnicorn\SymfonyKernel\Model\Model;

class BaseService extends BaseController
{
    /**
     * @var Model
     */
    protected $m_model;

    /**
     * @param $data
     * @return bool
     */
    public function insert($data){
        return $this->m_model->insert($data);
    }

    /**
     * @param $id
     * @param $data
     * @return bool
     */
    public function update($id, $data)
    {
        if (empty($id)){
            return false;
        }

        return $this->m_model->update($id, $data);
    }

    /**
     * @param $id
     * @return bool|int
     */
    public function delete($id)
    {
        if (empty($id)){
            return false;
        }

        return $this->m_model->delete($id);
    }

    /**
     * @param array $rule
     * @param int $isLimit
     * @param int $isSetList
     * @return array|mixed
     */
    public function getList($rule = [], $isLimit = 1,$isSetList = 1 ){
        if ($isLimit == 0){
            $OM = [Model::OM_ALL_LIST, Model::OM_COUNT];
        }else{
            $OM = [Model::OM_LIMIT_LIST, Model::OM_COUNT];
        }

        $list = $this->m_model->getList($rule, $OM);
        if ($isSetList == 1){
            $list['info'] = $this->m_model->setList($list['info'], $this->m_model);
        }

        return $list;
    }

    /**
     * @param array $rule
     * @return mixed
     */
    public function getCount($rule = [])
    {
        return $this->m_model->getList($rule, Model::OM_COUNT)['count'];
    }

    /**
     * @param array $rule
     * @return mixed
     */
    public function getSql($rule = [])
    {
        return $this->m_model->getList($rule, Model::OM_SQL)['sql'];
    }

    /**
     * @param array $rule
     * @return mixed
     */
    public function getSqlArray($rule = [])
    {
        return $this->m_model->getList($rule, Model::OM_SQL_ARRAY)['sql_array'];
    }

    /**
     * @param $column
     * @param array $rule
     * @param int $isSetInfo
     * @return array|bool|mixed
     */
    public function getColumn($column, $rule = [], $isSetInfo = 0)
    {
        $column = $this->m_model->getColumn($column, $rule);

        if ($isSetInfo == 1){
            $column = $this->m_model->setInfo($column);
        }
        return $column;
    }

    /**
     * @param array $rule
     * @param int $isSetInfo
     * @return array
     */
    public function getAssoc($rule = [], $isSetInfo = 0)
    {
        $info = $this->m_model->getAssoc($rule);

        if ($isSetInfo == 1){
            $info = $this->m_model->setInfo($info);
        }

        return $info;
    }

    /**
     * @param $id
     * @param array $rule
     * @param int $isSetInfo
     * @return array|mixed
     */
    public function getInfo($id, array $rule = [], $isSetInfo = 0)
    {
        if ($id === null || $id === ''){
            $id = 0;
        }

        $info = $this->m_model->getInfo($id, $rule);

        if ($isSetInfo == 1){
            $info = $this->m_model->setInfo($info);
        }

        return $info;
    }

    /**
     * @param $search_val
     * @param string $search_field
     * @param array $rule
     * @param null $exclude_primary_key_id
     * @return bool|mixed
     */
    public function isExist($search_val,  $search_field = '', $rule = [] ,$exclude_primary_key_id = null)
    {
        return $this->m_model->is_exist($search_val, $search_field, $rule,$exclude_primary_key_id);
    }

    /**
     * @param array $rule
     * @param bool $def
     * @return bool|mixed
     */
    public function getLastId($rule = [], $def = false)
    {
        return $this->m_model->getId($rule, $def);
    }

    /**
     * 不提供事物管理 只满足简单需求调用 多部操作需重写该函数
     *
     * @param $id
     * @param $data
     * @return bool|mixed
     */
    public function insertOrUpdate($id,$data)
    {
        if (empty($id)){
            $insert_res = $this->insert($data);
            if (empty($insert_res)){
                return false;
            }
            $id = $this->getLastId();
        }else{
            $update_res = $this->update($id,$data);
            if (empty($update_res)){
                return false;
            }
        }

        return $id;
    }

    public function updateOther($data, $query)
    {
        self::$conn->update($this->m_model->getTableName(),$data,$query);
    }
}